<?php

namespace app\admin\controller;

use app\admin\model\Info as InfoModel;

class Info extends Common
{
    //首页
    public function index() {
        return $this->fetch('index');
    }

    //用户预约列表
    public function lst(){
        return $this->fetch('lst');
    }

    public function test(){
        $data = [
            'username'  =>  '迈克',
            'start_time'=>  8,
            'end_time'  =>  10,
            'time_count'=>  2,
            'seat_number'=> 'A5',
        ];
        $info = new InfoModel();
        $res = $info->save($data);
        if ($res) {
            echo "插入成功！";
        }else {
            echo  "插入失败";
        }
    }

    //用户预约信息列表数据接口
    public function getDatas() {
        $page = $this->request->param('page');
        $limit = $this->request->param('limit');

        $condition = $this->request->param('condition'); //查询的字段
        $content = $this->request->param('content'); //查询的内容

        $start = ($page-1)*$limit;

        //查询当天的数据
        $today = date('Y-m-d');
        if ($condition) {
            //搜索
            $data = InfoModel::whereLike([$condition=>'%'.$content.'%','start_time'=>$today.'%'])->limit($start,$limit)->select();
            $count = InfoModel::whereLike([$condition=>'%'.$content.'%','start_time'=>$today.'%'])->count();
        }else {
            $data = InfoModel::whereLike('start_time',$today.'%')->limit($start,$limit)->select();
            $count = count(InfoModel::whereLike('start_time',$today.'%')->all());
        }

        $info = [
            'code' => 0,
            'msg'  => '查询成功',
            'data' => $data,
            'count'=> $count
        ];
        echo json_encode($info);
        die;
    }

    //添加信息
    public function add() {
        if($this->request->isAjax()) {
            $data = [
                'username'       =>  $this->request->post('username'),
                'start_time'     =>  $this->request->post('start_time'),
                'end_time'       =>  $this->request->post('end_time'),
                'time_count'     =>  $this->request->post('end_time')-$this->request->post('start_time'),
                'seat_number'    =>  $this->request->post('seat_number'),
                'is_card'        =>  $this->request->post('is_card'),
            ];
            $info = new InfoModel();
            $res = $info->save($data);
            if ($res) {
                $this->success('添加成功','admin/info/lst');
            }else {
                $this->error('添加失败！');
            }
        }

        $selectTime = [8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23];
        $this->assign('selectTime',$selectTime);

        return $this->fetch('add');
    }

    //修改信息
    public function edit() {
        //提交修改后进行数据修改
        if ($this->request->isAjax()) {
            $data = [
                'id'            =>  $this->request->post('id'),
                'username'      =>  $this->request->post('username'),
                'start_time'    =>  $this->request->post('start_time'),
                'end_time'      =>  $this->request->post('end_time'),
                'time_count'    =>  $this->request->post('end_time')-$this->request->post('start_time'),
                'seat_number'   =>  $this->request->post('seat_number'),
                'is_card'       =>  $this->request->post('is_card')
            ];
            $info = new InfoModel();
            $res = $info->isUpdate(true)->save($data);
            if ($res) {
                $this->success('修改成功','admin/info/lst');
            }else {
                $this->error('修改失败！');
            }
        }

        //接收get传递的id
        $id = $this->request->param('id');
        //动态获取器，截取开始时间，结束时间
        $data = InfoModel::withAttr(
            [
                'start_time'=>function ($value){
                return intval(substr($value,11,2));
                },
                'end_time'=>function ($value) {
                    return intval(substr($value, 11, 2));
                }
        ])->where('id',$id)->find();
        $datas = [
            'selectTime' => [8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23],
            'data'       => $data
        ];
        $this->assign($datas);
        return $this->fetch('edit');
    }

    //删除
    public function del() {
        if ($this->request->isAjax()) {
            $id = $this->request->post('id');
            $res = InfoModel::get($id)->delete();
            if ($res) {
                $this->success('删除成功~', 'admin/info/lst');
            }else {
                $this->error('删除失败！');
            }
        }
    }
}
